﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class crtSwitch : MonoBehaviour
{
    //public Texture2D[] crts;
    public AudioClip switchClip;
    public GameObject playerList;
    GameObject[] models;
    public static GameObject modelResult;
    //public UnityEngine.UI.RawImage crt_bl;
    private int index;
    public Transform parent;

    void Start()
    {
        models = playerList.GetComponent<modelList>().models;

        SetModel();
    }

    public void LeftNext()
    {
        index--;
        SetModel();
        this.GetComponent<AudioSource>().PlayOneShot(switchClip);
        //crt_bl.texture = crts[Mathf.Abs(index) % crts.Length];
    }

    public void RightNext()
    {
        index++;
        SetModel();
        this.GetComponent<AudioSource>().PlayOneShot(switchClip);
        //crt_bl.texture = crts[Mathf.Abs(index) % crts.Length];
    }


    void SetModel()
    {
        if (index > models.Length - 1)
        {
            index = 0;
        }
        else if (index < 0)
        {
            index = models.Length - 1;
        }

        for (int i = 0; i < parent.transform.childCount; i++)
        {
            Destroy(parent.transform.GetChild(i).gameObject);
        }

        GameObject player = Instantiate(models[index], parent);
        player.GetComponent<Animator>().enabled = false;
        player.transform.SetParent(parent.transform);


        Player.PlayerPrefab = models[index];
    }
}
